package com.example.httprequestdemos;

public class sendPostDemo {
    /**
     * @param url 接口地址
     * @param headers 消息头
     * @param data 消息体
     * @return
     */
//    public static String sendPost(String url, Map<String, String> headers, String data) {
//        String response = null;
//        try {
//            CloseableHttpClient httpclient = null;
//            CloseableHttpResponse httpresponse = null;
//            try {
//                httpclient = HttpClients.createDefault();
//                HttpPost httppost = new HttpPost(url);
//                StringEntity stringentity = new StringEntity(data, ContentType.create("application/json", "UTF-8"));
//                httppost.setEntity(stringentity);
//                // 循环添加header
//                Iterator headerIterator = headers.entrySet().iterator();
//                while (headerIterator.hasNext()) {
//                    Entry<String, String> elem = (Entry<String, String>) headerIterator.next();
//                    httppost.addHeader(elem.getKey(), elem.getValue());
//                }
//                //发post请求
//                httpresponse = httpclient.execute(httppost);
//                //utf-8参数防止中文乱码
//                response = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
//            } finally {
//                if (httpclient != null) {
//                    httpclient.close();
//                }
//                if (httpresponse != null) {
//                    httpresponse.close();
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return response;
//    }
}
